<template>
  <div>
    <!-- 头部搜索和新增用户按钮 -->
    <el-row :gutter="10" class="mb-20">
      <el-col :span="6">
        <el-input v-model="searchText" placeholder="搜索用户" aria-label="搜索用户"></el-input>
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="searchUsers" aria-label="查询">查询</el-button>
      </el-col>
      <el-col :span="6">
        <el-button type="primary" @click="showAddUserDialog" aria-label="新增用户"
          >新增用户</el-button
        >
      </el-col>
    </el-row>

    <!-- 用户列表 -->
    <el-table :data="userData" border>
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="organization" label="机构"></el-table-column>
      <el-table-column prop="role" label="角色"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column prop="status" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="text" size="small" @click="editUser(row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteUser(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      v-model:currentPage="currentPage"
      :total="totalUsers"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, prev, pager, next, jumper"
      :page-sizes="[10]"
      class="pagination-right-bottom"
    ></el-pagination>
    <!-- 新增用户弹窗 -->
    <el-dialog
      title="新增用户"
      :visible="addUserDialogVisible"
      @update:visible="addUserDialogVisible = $event"
      width="30%"
      :close-on-click-modal="false"
      @close="clearAddUserForm"
    >
      <el-form :model="addUserForm" ref="addUserForm" :rules="addUserRules" label-width="100px">
        <el-form-item aria-label="用户名" prop="username">
          <el-input v-model="addUserForm.username"></el-input>
        </el-form-item>
        <el-form-item aria-label="邮箱" prop="email">
          <el-input v-model="addUserForm.email"></el-input>
        </el-form-item>
        <el-form-item aria-label="手机号" prop="phone">
          <el-input v-model="addUserForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addUserDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addUser">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'UserManagement',
  data() {
    return {
      searchText: '',
      userData: [
        {
          id: 1,
          username: '张三',
          organization: '公司A',
          role: '管理员',
          email: 'zhangsan@example.com',
          phone: '1234567890',
          status: 'Active'
        },
        {
          id: 2,
          username: '李四',
          organization: '公司B',
          role: '普通用户',
          email: 'lisi@example.com',
          phone: '2345678901',
          status: 'Active'
        },
        {
          id: 3,
          username: '王五',
          organization: '公司C',
          role: '管理员',
          email: 'wangwu@example.com',
          phone: '3456789012',
          status: 'Inactive'
        },
        {
          id: 4,
          username: '张三',
          organization: '公司A',
          role: '管理员',
          email: 'zhangsan@example.com',
          phone: '1234567890',
          status: 'Active'
        },
        {
          id: 5,
          username: '李四',
          organization: '公司B',
          role: '普通用户',
          email: 'lisi@example.com',
          phone: '2345678901',
          status: 'Active'
        },
        {
          id: 6,
          username: '王五',
          organization: '公司C',
          role: '管理员',
          email: 'wangwu@example.com',
          phone: '3456789012',
          status: 'Inactive'
        },
        {
          id: 7,
          username: '王五',
          organization: '公司C',
          role: '管理员',
          email: 'wangwu@example.com',
          phone: '3456789012',
          status: 'Inactive'
        },
        {
          id: 8,
          username: '张三',
          organization: '公司A',
          role: '管理员',
          email: 'zhangsan@example.com',
          phone: '1234567890',
          status: 'Active'
        },
        {
          id: 9,
          username: '李四',
          organization: '公司B',
          role: '普通用户',
          email: 'lisi@example.com',
          phone: '2345678901',
          status: 'Active'
        },
        {
          id: 10,
          username: '王五',
          organization: '公司C',
          role: '管理员',
          email: 'wangwu@example.com',
          phone: '3456789012',
          status: 'Inactive'
        }
      ], // 用户数据
      totalUsers: 100, // 总用户数
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示条数
      addUserDialogVisible: false, // 新增用户弹窗是否可见
      addUserForm: {
        // 新增用户表单数据
        username: '',
        email: '',
        phone: ''
      },
      addUserRules: {
        // 新增用户表单验证规则
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      }
    }
  },
  methods: {
    showAddUserDialog() {
      this.addUserDialogVisible = true
    },
    clearAddUserForm() {
      this.$refs.addUserForm.resetFields()
    },
    addUser() {
      // 添加用户逻辑
      // 示例：发送请求或调用API添加用户
      console.log('新增用户：', this.addUserForm)
      // 关闭弹窗
      this.addUserDialogVisible = false
    },
    editUser(user) {
      // 编辑用户逻辑
      console.log('编辑用户：', user)
      // 示例：打开编辑用户弹窗，填充表单数据
    },
    deleteUser(user) {
      // 删除用户逻辑
      console.log('删除用户：', user)
      // 示例：发送请求或调用API删除用户
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
    },
    searchUsers() {
      // 处理搜索用户的逻辑
      console.log('搜索用户：', this.searchText)
      // 示例：发送请求或调用API搜索用户
    }
  }
}
</script>

<style>
.mb-20 {
  margin-bottom: 20px;
}
.pagination-right-bottom {
  position: fixed;
  bottom: 20px;
  right: 20px;
}
</style>
